var apitool = require('../../../../xc/sys/apitool.js')
var app = getApp();
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  // properties: {
  //   ishow: { // 属性名
  //     type: Boolean,
  //     value: true
  //   },
  // },

  /**
   * 组件的初始数据
   */
  data: {
    fileList: [],
    ispost: false,
    xcmodel: [],
    ishow: false,
    ischose: false,
    orderinfo: {},
    isfirst: true,
    rate: 5,
    contents: '亲，您对这个商品满意码？你的评价会帮助我们选择更好的商品',
    actionsdata: [{
        name: '不想要了'
      },
      {
        name: '描述与商品不符'
      },
      {
        name: '地址填错'
      },
      {
        name: '多拍，错拍'
      },
      {
        name: '其它'
      }
    ],
    CustomBar: app.globalData.CustomBar,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    chosechange() {
      this.setData({
        ischose: !this.data.ischose
      })

    },
    focustextarea() {
      if (this.data.isfirst) {
        this.setData({
          contents: '',
          isfirst: false
        })
      }

    },
    //contents  contents
    empmodel() {
      var newmodel = {
        ordernum: '',
        strtype: '',
        reason: '', //原因
        imglist: [],
        proid: 0,
        remark: "" //备注
      }
      return newmodel

    },

    showmodel: function (orderinfo, strtype) {
      var updatemodel = {}
      if (this.data.orderinfo == null || this.data.orderinfo.id != orderinfo.id) {
        updatemodel["orderinfo"] = orderinfo;
        var xcmodel = [];
        var fileList = [];
        for (var a in orderinfo.orderinfo) {
          xcmodel.push({
            rate: 5,
            contents: "",
            imglist: [],
            proid: orderinfo.orderinfo[a].proid
          })
          fileList.push([]);
        }
        updatemodel['xcmodel'] = xcmodel
        updatemodel["fileList"] = fileList
        if (!this.data.isfirst) {
          updatemodel["contents"] = ""
        }
      }
      updatemodel["ishow"] = true
      updatemodel["ispost"] = false

      this.setData(updatemodel)
      return
      ///orderinfo



      var updatemodel = {};
      if (!strtype) {
        strtype = "order"
      }
      var xcmodel = this.data.xcmodel
      if (xcmodel && xcmodel.ordernum == ordernum && xcmodel.strtype == strtype) {

      } else {
        xcmodel = this.empmodel()
        xcmodel['ordernum'] = ordernum
        xcmodel['strtype'] = strtype
        updatemodel["xcmodel"] = xcmodel
        updatemodel["fileList"] = []
      }
      updatemodel["ishow"] = true
      this.setData(updatemodel)
    },
    inputdata: function (ev) {
      var dataset = ev.currentTarget.dataset
      //  var vfield=dataset.idx+"."+dataset.field   
      this.data.xcmodel[dataset.idx][dataset.field] = ev.detail
      //  var field= ev.currentTarget.dataset.field;
      //  var upatemodel={}
      //   upatemodel['xcmodel.'+field]=ev.detail
      //  this.setData(upatemodel) 
    },
    showPopup: function () {
      this.setData({
        cidshow: true
      })
    },
    onClose: function () {
      this.setData({
        cidshow: false
      })

    },
    onSelect: function (ev) {
      var upatemodel = {}
      upatemodel['xcmodel.reason'] = ev.detail.name
      this.setData(upatemodel)
    },
    Backfunc: function () {
      this.setData({
        ishow: false
      })
    },
    saveform(event) {
      var that = this
      var savemodel = {}
      var xcmodel = this.data.xcmodel
      // console.log(xcmodel)
      var valide=true;
      for(var i in xcmodel){
        if(!xcmodel[i].contents){
          valide=false;
        }
      }
      if(!valide){
        wx.showToast({
          icon:"none",
          title: '每个产品请至少填写评价',
        })
        return false
      }
      
      console.log(xcmodel.contents)
      var proids = [];
      var orderinfo = this.data.orderinfo;
      orderinfo.orderinfo.forEach(item => {
        proids.push(item.proid)
      })
      savemodel["proids"] = proids;
      savemodel["ordernum"] = orderinfo["ordernum"]
      savemodel["isreal"] = this.data.ischose ? "-1" : "1"
      savemodel["data"] = xcmodel
      // console.log(savemodel);
      // return
      this.setData({
        ispost: true
      })
      var urlpara = {
        do: 'order_comment',
        'op': "add"
      }
      apitool.post(urlpara, {
        xc: JSON.stringify(savemodel)
      }).then(res => {
        if (res["status"] == 1) {
          that.triggerEvent("aftersucess", savemodel)
          that.setData({
            ispost: false,
            ishow: false
          })
          wx.showToast({
            icon: 'success',
            title: "操作成功"
          })
        } else {
          wx.showToast({
            title: res.message,
          })
        }
      })
      return;

      apitool.post(urlpara, {
        xc: JSON.stringify(this.data.xcmodel)
      }).then(res => {

        if (res["status"] == 1) {
          that.triggerEvent("aftersucess", this.data.xcmodel)
        } else {
          wx.showToast({
            title: res.message,
          })
        }
      })

    },

    savefilefunc(file, idx) {
    
  
      if(file.path){
        this.uploadimg(file,idx)
      }else{
        for(var i in file){
          this.uploadimg(file[i],idx)
        }
      }
     

    },
    uploadimg(file,idx){
      var app = getApp()
  
      var that = this
      app.toolv2.uploadFile(file.path).then(function (item) {
    
         console.log("ddddddddddddd")
         console.log(item)

        var fileList = that.data.fileList[idx];
        fileList.push({
          ...file,
          url: item.obj.fullpath
        });

        that.data.fileList[idx] = fileList
        var updatemodel = {}
        updatemodel["fileList"] = that.data.fileList
        var imglist = [];
        fileList.forEach(item => {
          imglist.push(item.url)
        })
        that.data.xcmodel[idx].imglist = imglist
        that.setData(updatemodel)
       
      })
    },

    afterRead(event) {
      console.log("event")
      console.log(event)
      var dataset = event.currentTarget.dataset
      var that = this
      const {
        file
      } = event.detail
      if (file.constructor == Array) {
        file.forEach(item => {
          that.savefilefunc(item, dataset.idx)
        })
      } else {
        that.savefilefunc(file, dataset.idx)
      }
    },
    afterdelete(e) {

      var that = this;
      var idx = e.currentTarget.dataset.idx;

      var fileList = that.data.fileList
      var index = e.detail.index;

      fileList[idx].splice(index, 1);

      this.setData({
        fileList: fileList,
      });
      var imglist = [];
      fileList[idx].forEach(item => {
        imglist.push(item.url)
      })
      that.data.xcmodel[idx].imglist = imglist

    },
    // afterRead(event) {
    //   const { file } = event.detail;
    //   var that = this
    //   var app = getApp()
    //   var dataset = event.currentTarget.dataset
    //   app.toolv2.uploadFile(file.path).then(function (item) {


    //     var fileList = that.data.fileList[dataset.idx]
    //     fileList.push({ ...file, url: item.obj.fullpath })
    //     that.data.fileList[dataset.idx] = fileList
    //     var updatemodel = {}
    //     updatemodel["fileList"] = that.data.fileList
    //     var imglist = [];
    //     fileList.forEach(item => {
    //       imglist.push(item.url)
    //     })
    //     that.data.xcmodel[dataset.idx].imglist = imglist
    //     that.setData(updatemodel)



    //   })
    // }
  }
})